package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.UUID;

@Api(tags = "通用接口")
@RestController
@RequestMapping("/admin/common")
@Slf4j
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;
    /*
    * 阿里云oss对象存储
    * */
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result<String> aliYunOSS(MultipartFile file) throws Exception {
        log.info("上传文件{}",file);
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(i);
        String objectName = UUID.randomUUID() + substring;
        String fileUrl = aliOssUtil.upload(file.getBytes(), objectName);
        return Result.success(fileUrl);
    }
}
